<template>
    <div>
      <!--面包屑导航区-->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
      <el-card>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList()">
              <el-button slot="append" icon="el-icon-search" @click="getGoodsList()"></el-button>
            </el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="goAddpage()">添加商品</el-button>
          </el-col>
        </el-row>
        <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" show-overflow-tooltip label="商品名称"></el-table-column>
        <el-table-column width="90" prop="goods_price" label="商品价格"></el-table-column>
        <el-table-column width="90" prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column width="180" prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{scope.row.add_time | dateformat}}
          </template>
        </el-table-column>
        <el-table-column width="130px" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="openEditDialog(scope.row)"></el-button>
            <el-button size="mini" type="danger" icon="el-icon-remove" @click="removeById(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      </el-card>
      <!--编辑商品对话框-->
    <el-dialog title="编辑商品信息" :visible.sync="editDialogVisible" width="50%" @close="closeEditDialog()">
      <el-form :model="editForm" ref="editFormRef" label-width="80px">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoods">确 定</el-button>
      </span>
    </el-dialog>
    </div>
</template>

<script>
export default {
  data () {
    return {
      goodsList: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      editDialogVisible: false,
      editForm: {
      }
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 监听每页数量变化触发
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听当前页改变
    handleCurrentChange (newPagenum) {
      this.queryInfo.pagenum = newPagenum
      this.getGoodsList()
    },
    // 打开编辑对话框
    openEditDialog (Goods) {
      console.log(Goods)
      this.editForm = Goods
      console.log(this.editForm)
      this.editDialogVisible = true
    },
    // 关闭编辑对话框
    closeEditDialog () {
    },
    // 保存编辑商品
    async editGoods () {
      console.log(this.editForm)
      const { data: res } = await this.$http.put('goods/' + this.editForm.goods_id, {
        goods_name: this.editForm.goods_name,
        goods_price: this.editForm.goods_price,
        goods_number: this.editForm.goods_number
      })
      console.log(res)
    },
    // 删除单个商品
    async removeById (id) {
      const confirmRes = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => { return err })
      if (confirmRes !== 'confirm') return this.$message.info('你取消了删除商品的操作')
      const { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) return this.$message.error('删除数据失败')
      this.$message.success('删除数据成功')
      this.getGoodsList()
    },
    // 点击添加商品跳转到添加页面
    goAddpage () {
      this.$router.push('/add')
    }
  }
}
</script>

<style lang="less" scoped>
</style>
